package com.yitiao.base.collection;

import java.util.HashMap;

public class TestCode {
    /**
     * 求一个字符串中的最长无重复子串长度，
     * 比如输入：abcdedafg
     * 输出：5
     * 解释：abcde这是一个最长的无重复子串，同理edafg也是。所以最长的无重复子串是5
     */
    public static void main(String[] args) {
        String s = "abcdedafgipqrsd";
        HashMap<Character, Integer> map = new HashMap<>();
        int left = 0;
        int right = 0;
        int maxLen = 0;
        while (right < s.length()) {
            char item = s.charAt(right);
            if (map.containsKey(item)) {
                left = Math.max(left, map.get(item) + 1);
            }
            map.put(s.charAt(right), right);
            maxLen = Math.max(maxLen, right - left + 1);
            right++;
        }
        System.out.println(maxLen);
    }
}
